package com.ghck.server.dao;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.ghck.server.dao.provider.MessageMapperProvider;
import com.ghck.server.model.Message;
/**
 *消息表
 * @author chenxuan
 *
 */
 public interface MessageMapper {
 	/**
 	* 通过ids查询
 	*/
 	@Select("select * from t_message where id = #{id}")
 	public Message  findById(Integer id);
 	
 	@Select("select a.*,b.user_id,b.is_read from t_message a,r_user_message b where b.user_id = #{userId} and a.id = #{id}")
 	public Message findByUserIdAndId(@Param("userId") Integer userId,@Param("id") Integer id);
 	
 	@Options(useGeneratedKeys=true)
 	@Insert("insert into t_message(title,content,is_push) values(#{title},#{content},#{isPush})")
 	public Integer insert(Message message);
 	
 	@Options(useGeneratedKeys=true,keyProperty="rId")
 	@Insert("insert into r_user_message(user_id,message_id) values(#{userId},#{id})")
 	public Integer insertUserMeesage(Message message);
 	
 	@SelectProvider(type=MessageMapperProvider.class,method="list")
 	public List<Message> list(Message message);
 	
 	@SelectProvider(type=MessageMapperProvider.class,method="myList")
 	public List<Message> myList(Message message);
 	
 	@Select("select count(1) from r_user_message where user_id = 4 and is_read = 0")
 	public Integer unreadCount(Integer userId);
 	
 	@Update("update r_user_message set is_read = 1 where id = #{messageId}")
 	public Integer markAsRead(Integer messageId);
 	
 	
 	

}	